package com.power.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.power.model.jdao.IRoleDao;
import com.power.model.mdao.RoleMapper;
import com.power.model.pojo.Role;
/**
 * 角色的搜索  添加  删除  修改
 * @author Administrator
 *
 */
@Service
public class RoleService {
	@Autowired
	RoleMapper mapper;
	@Autowired
	IRoleDao dao;
	List<Role> list;
	Role r;
	/**
	 *角色管理 查询所有角色
	 * @return
	 */
	public List<Role> selectAll(Integer pageNo,Integer pageSize){
		PageHelper.startPage(pageNo, pageSize);
		list=mapper.selectAll();
		
		return list;
	}
	/**
	 * 角色配置权限查询所有角色
	 * @return
	 */
	public List<Role> selectAlls(){
		list=mapper.selectAll();
		return list;
	}
	/**
	 * 条件模糊查询
	 * @param pageNo
	 * @param pageSize
	 * @param rname
	 * @param isstatus
	 * @return
	 */
	public List<Role> selectBywhere(Integer pageNo,Integer pageSize,String rname,int isstatus){
		PageHelper.startPage(pageNo, pageSize);
		list=mapper.selectBywhere(rname,isstatus);
		System.out.println(list.toString());
		return list;
	}
	/**
	 * 添加角色
	 * @param rname
	 * @param isstatus
	 * @param createby
	 */
	public void addRole(String rname,Integer isstatus,String createby) {
		r=new Role(createby,new Date(),isstatus,rname);
		dao.save(r);
	}
	/**
	 * 删除角色
	 * @param rid
	 */
	public void delRole(Integer rid) {
		Role r=mapper.selectByRid(rid);
		if(r.getIsStatus()==1) {
			r.setIsStatus(0);
			dao.save(r);
		}else if(r.getIsStatus()==0) {
			r.setIsStatus(1);
			dao.save(r);
		}
		
	}
	public void updataRole(Integer rid,Integer isstatus) {
		mapper.updateRole(rid, isstatus);
	}
	public List<Role> selectRoleRname(){
		list=mapper.selectRoleRname();
		return list;
	}
	public Role selectByRid(Integer rid) {
		return mapper.selectByRid(rid);
	}
	public List<Role> selectByUid(Integer uid){
		list=mapper.selectByUid(uid);
		return list;
	}
	public boolean selectByRname(String rname){
		if(mapper.selectByRname(rname).size()>0) {
			return false;
		}else {
			return true;
		}
	}
	public void update(int rid,Integer []mids) {
		mapper.deleteMenuByRid(rid);
		mapper.addMenuRole(rid, mids);
	}

	public List<Role> selectMenuByRid(Integer rid){
		return mapper.selectMenuByRid(rid);
	}
}	
